package net.elive.peer.assist;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import net.elive.tools.Contants;
import net.elive.tools.MessageCheck;
import net.elive.tools.MessageType;
import net.elive.tools.RandomNum;
import net.elive.utils.LogUtils;

import org.apache.commons.logging.Log;

/**
 * 协服务器
 * @author Administrator
 *
 */
public class AssistServer implements Runnable {
	Log log =LogUtils.getLog(AssistServer.class);
	/**
	 * 数据包协议套接字
	 */
	private DatagramSocket ds;
	/**
	 * 主线程
	 */
	private Thread  mainThread;
	
	public AssistServer(){
		try {
			ds = new DatagramSocket();
			Contants.ASSISTSERVERPORT = ds.getLocalPort();
			log.info(">>>>>>>>>>>初始化协服务器失败>>>>>>>>>>>");
			log.info("协服务器信息如下:"+ds.toString());
		} catch (SocketException e) {
			log.error("<<<<<<<<<<<<初始化协服务器失败<<<<<<<<<");
			e.printStackTrace();
		}
		mainThread = new Thread(this);
		mainThread.start();			//启动该线程
		
	}
	@Override
	public void run() {
		byte[] data =new  byte[1000];
		DatagramPacket dp = new DatagramPacket(data,data.length);
		while(true){
			try {
				ds.receive(dp);			//接收数据包
			} catch (IOException e) {
				e.printStackTrace();
				log.error(">>>>协服务器接受数据包出现异常:继续接收中.....");
				continue;
			}
			int sType = MessageCheck.checkSType(dp);		//获取消息类型
			if(sType == MessageType.sType_Assist_RegMessage){
				log.info("协服务器消息类型:"+sType);
				int sessionId;
				while(true){
					sessionId = RandomNum.randomInt();
					
				}
			}
		}
		
		
	}

}
